package com.csgc.camera.biz.other.activity

import android.content.Context
import android.content.Intent
import android.net.http.SslError
import android.os.Bundle
import android.webkit.SslErrorHandler
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient

import com.csgc.camera.base.BaseBindingActivity
import com.cysd.spsyxj.R
import com.cysd.spsyxj.databinding.ActivityWebviewBinding


import com.gyf.immersionbar.ktx.immersionBar

class WebViewActivity : BaseBindingActivity<ActivityWebviewBinding>(ActivityWebviewBinding::inflate){

    companion object{
        fun launch(context: Context,title:String){
            val intent = Intent(context,WebViewActivity::class.java)
            intent.putExtra("title",title)
            context.startActivity(intent)
        }
        @JvmStatic
        fun launch(context: Context,title:String,url:String){
            val intent = Intent(context,WebViewActivity::class.java)
            intent.putExtra("title",title)
            intent.putExtra("url",url)
            context.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        immersionBar {

            fitsSystemWindows(true)
            //白色状态栏，黑色字体
            statusBarColor(R.color.white)
            statusBarDarkFont(true)
        }
        val title = intent.getStringExtra("title")
        binding.tvTitleName.text = title
        setting()
        if (intent.hasExtra("url")){
            binding.wvWebview.loadUrl(intent.getStringExtra("url")?:"")
        }


        binding.ivTitleBack.setOnClickListener {
            finish()
        }
    }


    private fun setting(){
        binding.wvWebview.webViewClient = object : WebViewClient(){
            override fun onReceivedSslError(
                view: WebView?,
                handler: SslErrorHandler?,
                error: SslError?
            ) {
                handler?.proceed()
            }
        }

        val webSettings: WebSettings = binding.wvWebview.settings
        //设置开启javascript支持
        webSettings.javaScriptEnabled = true
        //设置支持缩放
        webSettings.setSupportZoom(true)
        //开启缩放工具（会出现放大缩小的按钮）
        webSettings.builtInZoomControls = false
        //WebView两种缓存（网页、H5）方式，此处网页不缓存
        webSettings.cacheMode = WebSettings.LOAD_NO_CACHE
        //允许JS打开新窗口（默认false）
        webSettings.javaScriptCanOpenWindowsAutomatically = true
        //打开本地缓存供JS调用
        webSettings.domStorageEnabled = true
        //webSettings.setAppCacheMaxSize(1024 * 1024 * 8);
        //H5缓存路径
        val absolutePath = applicationContext.cacheDir.absolutePath
        //是否允许WebView访问内部文件（默认true）
        webSettings.allowFileAccess = true
        //启动概述模式浏览界面，当页面宽度超过WebView显示宽度时，缩小页面适应WebView(默认false)
        webSettings.loadWithOverviewMode = true

        webSettings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
    }
}